﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ChromaticQFX
{
    public partial class EditCategories : Form
    {
        public EditCategories()
        {
            InitializeComponent();
        }

        private void EditCategories_Load(object sender, EventArgs e)
        {
            lstCategory.DataSource = MainForm.history.Categories;

            ActiveControl = txtCategory;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            MainForm.history.Categories.Add(txtCategory.Text);

            txtCategory.Text = "";

            lstCategory.DataSource = null;
            lstCategory.DataSource = MainForm.history.Categories;

            ActiveControl = txtCategory;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (lstCategory.SelectedItem == null) return;

            string SelectedCategory = lstCategory.SelectedItem.ToString();
            if (MainForm.history.FilterByCategory(SelectedCategory).Count > 0)
            {
                if (MessageBox.Show("This will clear the category of those transactions-- are you sure?", "Confirm deletion of Category in use", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) != DialogResult.OK) return;
            }

            foreach (QFXTransaction trans in MainForm.history.Transactions)
            {
                if (trans.Category == SelectedCategory) trans.Category = "";
            }

            MainForm.history.Categories.Remove(lstCategory.SelectedItem.ToString());

            lstCategory.DataSource = null;
            lstCategory.DataSource = MainForm.history.Categories;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
